package com.example.demo.utils.qrCodeUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: QRCodeUtil
 * Description:
 * Author: lxw
 * Date: 2025/7/22 13:46
 * History:
 * <author>          <time>          <version>          <desc>
 * lxw         2025/7/22 13:46   @Version 1.0        描述
 */
public class QRCodeUtil {


    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("二维码生成器");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,  200);
        frame.setLayout(new  BorderLayout());

        // 添加输入组件
        JPanel inputPanel = new JPanel();
        JTextField textField = new JTextField(20);
        JButton generateButton = new JButton("生成条形码");
        JButton generateButton2 = new JButton("生成二维码");

        inputPanel.add(new  JLabel("输入内容:"));
        inputPanel.add(textField);
        inputPanel.add(generateButton);
        inputPanel.add(generateButton2);

        // 添加事件处理
        generateButton.addActionListener(new  ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String content = textField.getText();
                if (!content.isEmpty())  {
                    generateBarcode(
                            content,
                            frame
                    );
                } else {
                    JOptionPane.showMessageDialog(
                            frame,
                            "请输入要生成二维码的内容",
                            "提示",
                            JOptionPane.WARNING_MESSAGE
                    );
                }
            }
        });
        // 添加事件处理
        generateButton2.addActionListener(new  ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String content = textField.getText();
                if (!content.isEmpty())  {
                    try {
                        generateQRCode(
                                content,
                                frame
                        );
                    } catch (WriterException ex) {
                        throw new RuntimeException(ex);
                    }
                } else {
                    JOptionPane.showMessageDialog(
                            frame,
                            "请输入要生成二维码的内容",
                            "提示",
                            JOptionPane.WARNING_MESSAGE
                    );
                }
            }
        });

        frame.add(inputPanel,  BorderLayout.CENTER);
        frame.setVisible(true);
    }

    /**
     * 生成二维码
     * @param content 二维码内容
     */
    public static JLabel generateQRCode(String content, Component  parentComponent) throws WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400);

        BufferedImage qrImage = new BufferedImage(encode.getWidth(), encode.getHeight(), BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < encode.getWidth(); x++) {
            for (int y = 0; y < encode.getHeight(); y++) {
                qrImage.setRGB(x, y, encode.get(x, y) ? 0x000000 : 0xFFFFFF);
            }
        }
        // 创建显示组件
        JLabel qrLabel = new JLabel(new ImageIcon(qrImage));
        JOptionPane.showMessageDialog(parentComponent, qrLabel, "二维码", JOptionPane.PLAIN_MESSAGE);

        return qrLabel;
    }

    /**
     * 生成一维码(Code128格式)
     * @param content 一维码内容
     * @return BufferedImage对象
     */
    public static JLabel generateBarcode(String content, Component  parentComponent) {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET,  "UTF-8");

        Code128Writer writer = new Code128Writer();
        BitMatrix matrix = writer.encode(content,  BarcodeFormat.CODE_128, 400, 200, hints);
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix);
        JLabel qrLabel = new JLabel(new ImageIcon(bufferedImage));
        JOptionPane.showMessageDialog(parentComponent, qrLabel, "一维码", JOptionPane.PLAIN_MESSAGE);
        return qrLabel;
    }
}
